<?php

namespace app\common\model\xiservice;

use think\Model;
use app\common\model\xiservice\Service;
use app\common\model\xiservice\Comment;
use app\common\model\xiservice\User as XiserviceUser;

class Wxtemplate extends Model
{


    // 表名
    protected $name = 'xiservice_wxtemplate';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text'
    ];


    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }


    public function getEventList()
    {
       // return ['order_come' => __('新订单提醒'), 'order_confirm' => __('接单提醒'), 'order_cancel' => __('订单取消提醒'), 'order_review' => __('订单评价通知'), 'order_start' => __('服务开始提醒'), 'change_worker' => __('服务人员变更提醒'), 'change_time' => __('服务时间变更提醒'), 'check_apply' => __('申请验收提醒'), 'check_finish' => __('验收结果通知')];
       return ['order_come' => __('新订单通知'), 'order_confirm' => __('施工队报价通知'), 
       'order_cancel' => __('入驻成功通知'), 'order_review' => __('订单取消通知'), 
       'order_start' => __('订单完成通知'), 'change_worker' => __('下单成功通知'), 
       'change_time' => __('接单通知'),];
        // 'check_apply' => __('申请验收提醒'), 'check_finish' => __('验收结果通知')
    
    }

    public function getItemList()
    {
        return ['order_no' => __('订单编号'), 'order_status' => __('订单状态'), 'service_name' => __('服务项目'), 'service_start_time' => __('服务时间'), 'worker_name' => __('服务人员'), 'check_time' => __('完工时间'),  'check_status' => __('验收状态'), 'score' => __('评分分数'), 'content' => __('评分内容'), 'remark' => __('自定义文本')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 组合信息
     * @param  [type] $order   订单信息
     * @param  [type] $openid  接收人openid
     */
    public function messageData($order, $openid, $event)
    {
        $template = $this->where('event', $event)->where('type', 1)->where('status', 1)->find();
        if (!$template) {
            return false;
        }
        $data['template_id'] = $template['template_id'];
        $data['touser'] = $openid;
        $data['page'] = $template['page_url'];
        $param = $order['param'];
        if($param)
        {
            $data['page'] = $data['page'].'?' . $param;
        }
        $list = json_decode($template['json'], true);
        $service = Service::where('id', $order['service_id'])->find();
        $comment = Comment::where('order_id', $order['id'])->find();
        $worker = XiserviceUser::where('user_id', $order['worker_id'])->find();

        if ($list) {
            foreach ($list as $key => $value) {
                $indexkey = str_replace('.DATA', '', $value['key']);
                $typeData = $this->findType($value['key'], $value['type'], $value['value'], $order, $service, $comment, $worker);
                $data['data'][$indexkey]['value'] = $typeData;
            }
        }

        return $data;
    }

    public function messageData1($checkInfo, $openid, $event){
        $template = $this->where('event', $event)->where('type', 1)->where('status', 1)->find();
        if (!$template) {
            return false;
        }
        $data['template_id'] = $template['template_id'];
        $data['touser'] = $openid;
        $data['page'] = $template['page_url'];
        $list = json_decode($template['json'], true);
    
        $data['data']['name1']['value'] = $checkInfo['contact_name'];
        $data['data']['phone_number2']['value'] =  $checkInfo['mobile'];
        $data['data']['date4']['value'] =  date('Y-m-d H:i:s',$checkInfo['createtime']);;
        $data['data']['thing7']['value'] = '您已成功入驻';
        $data['data']['phrase3']['value'] =  '通过';
        return $data;
    }
    public function messageData_audit_refuse($checkInfo, $openid, $event){
        $template = $this->where('event', $event)->where('type', 1)->where('status', 1)->find();
        if (!$template) {
            return false;
        }
        $data['template_id'] = $template['template_id'];
        $data['touser'] = $openid;
        $data['page'] = $template['page_url'];
        $list = json_decode($template['json'], true);
    
        $data['data']['thing2']['value'] = '入驻拒绝';
        $data['data']['time3']['value'] =   date('Y-m-d H:i:s');
        $data['data']['thing4']['value'] =   $checkInfo['reason'];
        return $data;
    }
    public function messageData_tixian_refuse($checkInfo, $openid, $event){
        $template = $this->where('event', $event)->where('type', 1)->where('status', 1)->find();
        if (!$template) {
            return false;
        }
        $data['template_id'] = $template['template_id'];
        $data['touser'] = $openid;
        $data['page'] = $template['page_url'];
        $list = json_decode($template['json'], true);
    
        $data['data']['thing2']['value'] = '提现拒绝';
        $data['data']['time3']['value'] =   date('Y-m-d H:i:s');
        $data['data']['thing4']['value'] =   $checkInfo['reason'];
        return $data;
    }

    public function messageData_score($checkInfo, $openid, $event,$cur_score){
        $template = $this->where('event', $event)->where('type', 1)->where('status', 1)->find();
        if (!$template) {
            return false;
        }
        $data['template_id'] = $template['template_id'];
        $data['touser'] = $openid;
        $data['page'] = $template['page_url'];
        $list = json_decode($template['json'], true);
    
        $data['data']['short_thing7']['value'] =$cur_score;
        $data['data']['number8']['value'] =$checkInfo['credit_score'];
        $data['data']['time3']['value'] =   date('Y-m-d H:i:s');
        $credit_score_name = $checkInfo['credit_score_name'];
        if(!$credit_score_name)
        {
            $credit_score_name ='';
        }
        $data['data']['thing6']['value'] =   $credit_score_name;
        return $data;
    }
    public function messageData_money($checkInfo, $openid, $event,$cur_money){
        $template = $this->where('event', $event)->where('type', 1)->where('status', 1)->find();
        if (!$template) {
            return false;
        }
        $data['template_id'] = $template['template_id'];
        $data['touser'] = $openid;
        $data['page'] = $template['page_url'];
        $list = json_decode($template['json'], true);
        $pay_money = $checkInfo['pay_money'];
        $data['data']['short_thing7']['value'] =$cur_money;
        $data['data']['number8']['value'] =$pay_money;
        $data['data']['time3']['value'] =   date('Y-m-d H:i:s');
        $data['data']['thing6']['value'] =   $checkInfo['pay_money_name'];
        $pay_money_name = $checkInfo['pay_money_name'];

        if(!$pay_money_name)
        {
            $pay_money_name ='';
        }
        $data['data']['thing6']['value'] = $pay_money_name;
        return $data;
    }
    /**
     * 匹配对应类型的内容
     */
    public function findType($key, $type, $content, $order, $service, $comment = null, $worker = null)
    {

        //service_start_time
        switch ($type) {
            case 'order_no':
                isset($order['order_no']) ? $data = $order['order_no'] : $data = '';
                break;
            case 'phone':
                isset($order['phone']) ? $data = $order['phone'] : $data = '暂无';
                break;
            case 'zjphone':
                $data = '0';
                break;
            case 'service_desc':
                isset($order['service_desc']) ? $data = $order['service_desc'].','.$order['remark'] : $data = '';
                break;   
            case 'service_process':
                $data = '等待验收';
                break;    
            case 'title':
                $data = '诊断意见';
                break;     
            case 'addr':
                isset($order['addr']) ? $data =$this->cutStringIfLongerThan17Chinese($order['addr']): $data = '';
                break;    
            case 'total_money':
                isset($order['quote_amount']) ? $data = $order['quote_amount'] : $data = '';
                break;
             case 'quote_amount':
                isset($order['quote_amount']) ? $data = $order['quote_amount'] : $data = '';
                break;    
            case 'service_start_time':
                isset($order['service_start_time']) ? $data = date('Y-m-d H:i',time()) : $data = '';
                break;
            case 'order_status':
                isset($order['order_status_text']) ? $data = $order['order_status_text'] : $data = '';
                break;
            case 'service_name':
                isset($service['name']) ? $data = $service['name'] : $data = '专家';
                break;
            case 'remark':
                isset($content) ? $data = $content : $data = '';
                break;
            case 'service_start_time':
                isset($order['service_start_time']) ? $data = date('Y-m-d H:i', $order['service_start_time']) : $data = '';
                break;
            case 'worker_name':
                isset($worker['contact_name']) ? $data = $worker['contact_name'] : $data = '';
                break;
            case 'score':
                isset($comment['rate']) ? $data = $comment['rate'] : $data = '';
                break;
            case 'content':
                isset($comment['content']) ? $data = $comment['content'] : $data = '';
                break;
            case 'check_time':
                isset($order['check_time']) ? $data = date('Y-m-d H:i', $order['check_time']) : $data = '';
                break;
            case 'check_status':
                isset($order['check_status_text'])  ? $data = $order['check_status_text'] : $data = '';
                break;
            default:
                $data = '';
                break;
        }

        return $data;
    }

    function cutStringIfLongerThan17Chinese($str) {
        $len = mb_strlen($str, 'UTF-8');
    
        if ($len > 17) {
            $str = mb_substr($str, 0, 17, 'UTF-8');
        }
    
        return $str;
    }
}
